from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from django.views import View
from bookTest.models import Book
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet, ModelViewSet
from rest_framework.decorators import action
from bookTest.serializers import BookSerializers
from rest_framework.generics import GenericAPIView
from drf_yasg.utils import swagger_auto_schema
from drf_yasg.openapi import Schema
from drf_yasg import openapi
from rest_framework.mixins import DestroyModelMixin
# from rest_framework.decorators import api_view, APIView
# Create your views here.


# class BookView(View):

#     def get(self, request):
#         books = Book.objects.all()
#         ret_list = []
#         for book in books:
#             ret_list.append({"name":book.name, 'price':book.price, 'createTime':book.createTime.strftime('%Y-%m-%d %H:%M:%S')}) # , default='%Y-%m-%d %H:%M:%S'
#         return JsonResponse(data=ret_list,safe=False, status=status.HTTP_200_OK)
                

# class BookView(APIView):
#     def get(self, request):
#         books = Book.objects.all()
#         s = BookSerializers(instance=books, many=True)
#         return Response(data=s.data, status=status.HTTP_200_OK)

    
#     def post(self, request):
#         s = BookSerializers(data=request.data)
#         if s.is_valid():
#             s.save()
#             return Response({'code': 100, 'msg': '新增数据成功', 'result': s.data})
#         else:
#             return Response({'code':101, 'msg':s.errors})



class BookView(GenericAPIView):

    queryset = Book.objects.all()
    serializer_class = BookSerializers

    @swagger_auto_schema(
        tags=['测试查询']
    )
    def get(self, request):
        objs = self.get_queryset()
        s = self.serializer_class(instance=objs, many=True)
        return Response(s.data)
    
    @swagger_auto_schema(
        tags=['测试查询']
    )
    def post(self, request):

        objs = self.get_serializer(data=request.data)
        if objs.is_valid():
            objs.save()
            return Response({'code': 100, 'msg': '新增成功', 'result': objs.data})
        else:
            return Response({'code': 101, 'msg': objs.errors})

    # @swagger_auto_schema(
    #     request_body=Schema(
    #         type=openapi.TYPE_OBJECT,
    #         properties=({
    #             'name':Schema(
    #                 type=openapi.TYPE_STRING,
    #                 description="名称"
    #             )
    #         })
    #     )
    # )
    # def post(self, request):
    #     objs = self.get_queryset()
    #     objs = objs.filter(name=request.data.get("name"))
    #     s = self.serializer_class(instance=objs, many=True)
    #     return Response(s.data)


class BookDestroyView(GenericAPIView, DestroyModelMixin):


    def destroy(self, request, *args, **kwargs):
        return super().destroy(request, *args, **kwargs)

    